// ATOMIC.h: interface for the ATOMIC class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ATOMIC_H__913921F8_AB72_4FC9_B3FE_3095F449A8FE__INCLUDED_)
#define AFX_ATOMIC_H__913921F8_AB72_4FC9_B3FE_3095F449A8FE__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class ATOMIC  
{
public:
	ATOMIC()
	{
		::InitializeCriticalSection(&m_cs);
	}

	~ATOMIC()
	{
		::DeleteCriticalSection(&m_cs);
	}
public:
	inline void StartAtomicOp()
	{
		::EnterCriticalSection(&m_cs);
	}
	inline void LeaveAtomicOp()
	{
		::LeaveCriticalSection(&m_cs);
	}

protected:
	CRITICAL_SECTION m_cs;
};

//  [12/7/2012 Pseudonym]
class cjMutex
{
public:
	cjMutex(BOOL binitialOwner, const char* name)
	{
		m_hmutex = ::CreateMutex(NULL,binitialOwner,name);
		bLocked = FALSE;
	};
	~cjMutex(){
		CloseHandle(m_hmutex);
	};

public:
	BOOL Lock(DWORD dwtimeout = INFINITE )
	{
		if(::WaitForSingleObject(m_hmutex,dwtimeout) == WAIT_TIMEOUT)
			return FALSE;
		bLocked = TRUE;
		return TRUE;
	};
	BOOL Unlock()
	{
		if(bLocked == TRUE)
			ReleaseMutex( m_hmutex);
		
		return TRUE;
	};
protected:
	HANDLE m_hmutex;
	BOOL bLocked;
};

#endif // !defined(AFX_ATOMIC_H__913921F8_AB72_4FC9_B3FE_3095F449A8FE__INCLUDED_)
